package com.biglead.admin.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.admin.mapper.PmsBrandMapper;
import com.biglead.admin.mapper.PmsProductAttributeMapper;
import com.biglead.admin.mapper.ProductMapper;
import com.biglead.admin.param.PmsBrandParam;
import com.biglead.admin.pojo.PmsBrand;
import com.biglead.admin.pojo.PmsProduct;
import com.biglead.admin.pojo.PmsProductAttribute;
import com.biglead.admin.pojo.PmsProductCategory;
import com.biglead.admin.service.PmsBrandService;
import com.biglead.admin.service.PmsProductAttributeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("brandService")
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<PmsBrand> listAllBrand() {
        return this.list(new LambdaQueryWrapper<PmsBrand>());
    }

    @Override
    public boolean createBrand(PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        return this.save(pmsBrand);
    }

    @Override
    @Transactional
    public boolean updateBrand(Long id, PmsBrandParam pmsBrandParam) {

        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        pmsBrand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        //更新品牌时要更新商品中的品牌名称
        productMapper.updateBrandName(id,pmsBrand.getName());

        return this.updateById(pmsBrand);
    }

    @Override
    public boolean deleteBrand(Long id) {
        return this.removeById(id);
    }

    @Override
    public Page<PmsBrand> listPageBrand(String keyword, Integer showStatus, Integer pageNum, Integer pageSize) {
        Page<PmsBrand> page = Page.of(pageNum, pageSize);
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件 parentId
        queryWrapper.like(StringUtils.isNotEmpty(keyword), PmsBrand::getName, keyword);
        queryWrapper.like(showStatus != null, PmsBrand::getShowStatus, showStatus);
        //发起查询
        this.page(page, queryWrapper);
        return page;
    }

    @Override
    public PmsBrand getBrand(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean deleteBrandFromList(List<Long> ids) {
        return false;
    }

    @Override
    public boolean updateShowStatus(List<Long> ids, Integer showStatus) {
        UpdateWrapper<PmsBrand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        PmsBrand productCategory = new PmsBrand();
        productCategory.setShowStatus(showStatus);
        boolean i = this.update(productCategory,updateWrapper);
        return i;
    }

    @Override
    public boolean updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        UpdateWrapper<PmsBrand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        PmsBrand productCategory = new PmsBrand();
        productCategory.setFactoryStatus(factoryStatus);
        boolean i = this.update(productCategory,updateWrapper);
        return i;
    }
}
